<script lang="ts" setup>
import {  ref } from 'vue'
import { useGoodsStore } from '@/stores/goods'
import { confirmOrderAPI, addOrderAPI } from '@/API/order'
import { useUserStore } from '@/stores/user'
import { showToast } from 'vant'
import {useAddressStore} from '@/stores/address'
import router from '@/router'
const addressStore = useAddressStore()
const userStore = useUserStore()

const goodsStore = useGoodsStore()
//筛选默认地址
const select = addressStore.list.find((item: { isDefault: any })=>item.isDefault)

const tel = ref(select.tel)
const name = ref(select.name)
const time = ref()
//筛选已选择的商品
const isSelectGoodsList = goodsStore.goodsList.filter((item) => item.flag)

//获取订单信息
const getConfirmOrder = async () => {
  console.log(time.value);
  const res = await confirmOrderAPI(userStore.info.userid,time.value)
  console.log(res)
}


// 删除购物车数据
// const delCar = (cartid: any) => {
//   goodsStore.delGoodsList(cartid)
// }


// 支付订单
const onSubmit = async () => {
  const res: any = await addOrderAPI(userStore.info.userid)
  console.log(res.time);
  
  if (res.code === '200') {
    time.value = res.time
    showToast('支付成功')
    getConfirmOrder()
    goodsStore.deleteCartItem()
    setTimeout(()=>{
      router.push('/car')
    },2000)
    
  }
}
</script>

<template>
  <van-nav-bar fixed title="支付页面" left-arrow @click-left="$router.go(-1)" />

  <div class="fixeds">
    <van-contact-card type="edit" :tel="tel" :name="name"   style="margin-top: 46px" />
  <van-swipe-cell v-for="item in isSelectGoodsList" :key="item.proid">
    <van-card class="goods-card" :num="item.discount" :price="item.originprice.toFixed(2)" :desc="item.proname" :thumb="item.img1" origin-price="10.00" />

    <template #right>
      <van-button square text="删除" type="danger" class="delete-button" />
    </template>
  </van-swipe-cell>
  </div>

  <van-submit-bar :price="goodsStore.total * 100" button-text="支付订单" @submit="onSubmit"  />
</template>

<style scoped lang="less">
.fixeds {
  margin-bottom: 50px;
}
.goods-card {
  margin: 0;
  background-color: white;
  border-bottom: 1px solid grey;
}

.delete-button {
  height: 100%;
}
</style>
